#ifndef __MYLOG_HPP__
#define __MYLOG_HPP__

#include <iostream>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <pthread.h>
#include <unistd.h>
#include <memory>
#include "Mutex.hpp"

namespace LogModule
{
    using namespace MutexMudule;
    const std::string gsep = "\n\r";


    class LogStrategy
    {
    public:
        ~LogStrategy() = default;
        virtual void SyncLog(const std::string& message) = 0;
    };

    class ConsoleLogStrategy : public LogStrategy
    {
    public:
        ConsoleLogStrategy()
        {}

        void SyncLog(const const std::string& message) override
        {
            GuardLock guardlock(_mutex);
            std::cout << message << gsep;
        }
    private:
        Mutex _mutex;
    };

    const std::string defaultpath = "./log/";
    const std::string defaultfile = "My.log";

    class FileLogStrategy : public LogStrategy
    {
    public:
        FileLogStrategy(std::string path, std::string file)
            :_file(file), _path(path)
        {

            GuardLock guardlock(_mutex);
            if(std::filesystem::exists(_path))
                return;
            try
            {
                std::filesystem::create_directories(_path);
            }
            catch(const std::filesystem::filesystem_error& e)
            {
                std::cerr << e.what() << '\n';
            }
        }

        void SyncLog(const std::string& message) override
        {
            GuardLock guardlock(_mutex);
            std::string filename = _path + (_path.back() == '/' ? "" : "/") + _file;
            std::ofstream out(filename, std::ios::app);
            if(!out.is_open())
                return;
            out << message << gsep;
            out.close();
        }
    private:
        Mutex _mutex;
        std::string _file;
        std::string _path;
    };

    enum class LogLevel
    {
        DEBUG,
        ERROR,
        FATAL,
        INFO,
        WARNING
    };

    std::string Level2Str(LogLevel level)
    {
        switch(level)
        {
            case LogLevel::DEBUG:
                return "DEBUG";
            case LogLevel::INFO:
                return "INFO";
            case LogLevel::WARNING:
                return "WARNING";
            case LogLevel::FATAL:
                return "FATAL";
            case LogLevel::ERROR:
                return "ERROR";
            default:
                return "UNKNOWN";
        }
    }

    std::string GetTimeStamp()
    {
        time_t curr = time(nullptr);
        struct tm curr_tm;
        localtime_r(&curr, &curr_tm);

        char timebuffer[128];

        snprintf(timebuffer, sizeof(timebuffer),"%4d:%2d:%2d %2d:%2d:%2d",
            curr_tm.tm_year + 1900,
            curr_tm.tm_mon + 1,
            curr_tm.tm_mday,
            curr_tm.tm_hour,
            curr_tm.tm_min,
            curr_tm.tm_sec
        );

        return timebuffer;
    }

    class Logger
    {
    public:
        Logger()
        {

        }
    
        void EnableFileStrategy()
        {
            _fflush_Strategy = std::make_unique<FileLogStrategy>();
        }

        void EnableConsoleStrategy()
        {
            _fflush_Strategy = std::make_unique<ConsoleLogStrategy>();
        }

        class LogMessage
        {
        public:
            LogMessage(LogLevel level,std::string src_name, int line_number, Logger& logger)
                :_logger(logger)
                ,_pid(getpid())
                ,_curr_time(GetTimeStamp())
                ,_src_name(src_name)
                ,_line_number(line_number)
                ,_level(level)
            {
                std::stringstream ss;
                ss << "[" << _curr_time << "]" 
                << "[" << Level2Str(_level) << "]" 
                << "[" << _pid << "]"
                << "[" << _line_number << "]"
                << "[" << _src_name << "]"
                << "-";
                _info += ss.str();
            }
            
            template <class T>
            LogMessage& operator<<(const T& message)
            {
                std::stringstream ss;
                ss << message;
                _info += ss.str();

                return *this;
            }

            
        private:
            LogModule::Logger& _logger;
            std::string _curr_time;
            int _line_number;
            LogLevel _level;
            pid_t _pid;
            std::string _src_name;
            std::string _info;
        };

        LogMessage operator()(LogLevel level, std::string name, int line)
        {
            return LogMessage(level, name, line, *this);
        }

        ~Logger()
        {

        }


    private:
        std::unique_ptr<LogStrategy> _fflush_Strategy;
    };

}

#endif